<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        {{ date | formatDate}}
    </div>

    <script src="https://unpkg.com/vue/dist/vue.js"></script>

    <script>
        // 在月份、日期、小时小于10的时间点前面补0
        var padDate = function (value) {
            return value < 10 ? '0' + value : value;
        }

        var app = new Vue({
            el: '#app',
            data: {
                date: new Date()
            },
            filters: {
                formatDate: function (value) {
                    let date = new Date(value);
                    let year = date.getFullYear();
                    let month = padDate(date.getMonth() + 1);
                    let day = padDate(date.getDate());
                    let hours = padDate(date.getHours());
                    let minutes = padDate(date.getMinutes());
                    let seconds = padDate(date.getSeconds());
                return year + '-' + month + '-' + day + '-' + hours + ':' + minutes + ':' + seconds;
                }
            },
            mounted: function(){ // 这里为什么使用箭头函数不行呢？深入了解
                var _this = this;
                this.timer = setInterval(()=>{
                    _this.date = new Date(); //修改数据date
                }, 1000);
            },
            beforeDestroy: ()=>{
                if(this.timer){
                    clearInterval(this.timer);
                }
            }
        })
    </script>
</body>

</html>